package com.lrkj.back.controller;

import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsPackageImformation;
import com.lrkj.back.entity.ZsQingxi;
import com.lrkj.back.entity.ZsWanglanpackage;
import com.lrkj.back.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by john on 2017/11/11.
 *
 * 脚本改变时间
 * */
@Controller
@RequestMapping("/updateZsPackage")
public class UpdateZsPackageImfor {

    @Autowired
    private ZsPackageImformationService zsPackageImformationService;

    @Autowired
    private TbPackageMubanService tbPackageMubanService;

    @Autowired
    private TbSeqService tbSeqService;

    @Autowired
    private ZsWanglanpackageService zsWanglanService;

    @Autowired
    private ZsQingxiService zsQingxiService;

    @Autowired
    private ZsWanglanpackageService zsWanglanpackageService;


    @RequestMapping("/updateCangjia")
    @ResponseBody
    public Map<String,Object> updateCangjia(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("packageOutcategory",1);
        List<TbPackageMuban> list = tbPackageMubanService.findPackage(map1);
        Random random = new Random();
        for(TbPackageMuban tbPackageMuban :list){
            String integer = tbPackageMuban.getPackageId();
            Map<String,Object> map2 = new HashMap<>();
            Date date = new Date();
            Date end = new Date();
            date.setMonth(3);
            date.setDate(1);
            end.setMonth(3);
            end.setDate(11);
            date.setHours(00);
            end.setHours(23);
            map1.put("startTime",date);
            map1.put("endTime",end);
            map2.put("startTime",date);
            map2.put("endTime",end);
            map2.put("packageId",integer);
            List<ZsPackageImformation> list1 = zsPackageImformationService.selectSinUp(map2);
            Long[] wlpacknos = tbSeqService.seqValBatch("wlpackno",list1.size());
            Long[] qingxinos = tbSeqService.seqValBatch("qingxino",list1.size());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            List<ZsQingxi> qingxis = new ArrayList<>();
            List<ZsWanglanpackage> wanglanpackages = new ArrayList<>();
            int i = 0;
            for(ZsPackageImformation zsPackageImformation:list1){
                if(zsPackageImformation!=null){
                    String wlpls = simpleDateFormat.format(new Date())+wlpacknos[i];
                    String qingxiLiushui =  "QX" + simpleDateFormat.format(new Date()) + qingxinos[i];
                    i++;
                    String  packageTiaoma = zsPackageImformation.getPackageTiaoma();
                    String shiyongLiushui  = zsPackageImformation.getShiyongLiushui();
                    ZsWanglanpackage zsWanglanpackage = new ZsWanglanpackage();
                    zsWanglanpackage.setPackageTiaoma(packageTiaoma);
                    zsWanglanpackage.setPackageZhuangtai(2);
                    zsWanglanpackage.setQingxiLiushui(qingxiLiushui);
                    zsWanglanpackage.setWlpackLiushui(wlpls);
                    zsWanglanpackage.setPackageId(String.valueOf(zsPackageImformation.getPackageId()));
                    zsWanglanpackage.setPackageName(zsPackageImformation.getPackageName());
                    ZsQingxi zsQingxi = new ZsQingxi();
                    String [] fafangren = new String[]{"EMP1028","EMP1012"};
                    int a = random.nextInt(1);

                    if(shiyongLiushui!=null){
                        Date shiyongTime = zsPackageImformation.getShiyongTime();
                        if(shiyongTime!=null){
                            long shiyongTimeLong =shiyongTime.getTime()+3*60*60*1000l+24*60*1000l;
                            Date fanxiTime = new Date(shiyongTimeLong);
                            Date fanxiJieshuTime = new Date(fanxiTime.getTime()+30*60*1000l);
                            zsQingxi.setQingxiKaishiTime(fanxiTime);
                            zsQingxi.setQingxiJieshuTime(fanxiJieshuTime);
                        }
                        zsQingxi.setQingxiLiushui(qingxiLiushui);
                        zsQingxi.setQingxiQingxirenTiaoma(fafangren[a]);
                        zsQingxi.setQingxiZhuangtai(2);
                        zsQingxi.setQingxiHege(1);
                        zsQingxi.setPackageFlag(2);
                        zsQingxi.setPackageNum(0);
                        zsQingxi.setQingxiShebeiTiaoma("1000003");
                        zsQingxi.setQingxiRuanjianTiaoma("1000052");
                        qingxis.add(zsQingxi);

                    }else{
//                        Date miejunJieshuTime = zsPackageImformation.getMiejunJieshuTime();
//                        if(miejunJieshuTime!=null){
//                            long shiyongTimeLong =miejunJieshuTime.getTime()+2*60*60*1000l;
//                            Date fanxiTime = new Date(shiyongTimeLong);
//                            Date fanxiJieshuTime = new Date(fanxiTime.getTime()+30*60*1000l);
//                            zsQingxi.setQingxiKaishiTime(fanxiTime);
//                            zsQingxi.setQingxiJieshuTime(fanxiJieshuTime);
//                        }
                    }

                    wanglanpackages.add(zsWanglanpackage);

                }
            }
            if(qingxis.size()!=0){
                zsQingxiService.insertBatch(qingxis);

            }
            if(wanglanpackages.size()!=0){
                zsWanglanService.insertBatch(wanglanpackages);
            }

        }
        map.put("msg","成功");
        return map;
    }

    @RequestMapping("/updateQingxi")
    @ResponseBody
    public Map<String,Object> updateQingxi(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();
        Date date = new Date();
        Date end = new Date();
        date.setMonth(10);
        date.setDate(13);
        end.setMonth(10);
        end.setDate(13);
        date.setHours(00);
        end.setHours(23);
        map1.put("startTime",date);
        map1.put("endTime",end);
        List<ZsPackageImformation> zsPackageImformationList = zsPackageImformationService.selectSinUp(map1);
        for(ZsPackageImformation zsPackageImformation :zsPackageImformationList){
            String tiaoma = zsPackageImformation.getPackageTiaoma();
            List<ZsWanglanpackage> zsWanglanpackages = zsWanglanpackageService.selectByPackageTiaoma(tiaoma);
            String qingxi = zsWanglanpackages.get(0).getQingxiLiushui();
            ZsQingxi zsQingxi = zsQingxiService.selectByPrimaryKey(qingxi);
            zsPackageImformation.setQingxiLiushui(qingxi);
            zsPackageImformation.setQingxiKaishiTime(zsQingxi.getQingxiKaishiTime());
            zsPackageImformation.setQingxiJieshuTime(zsQingxi.getQingxiJieshuTime());
            zsPackageImformation.setQingxiShenherenTime(zsQingxi.getQingxiShenherenTime());
            zsPackageImformation.setQingxiRuanjianTiaoma(zsQingxi.getQingxiRuanjianTiaoma());
            zsPackageImformation.setQingxiShenherenTiaoma(zsQingxi.getQingxiShenherenTiaoma());
            zsPackageImformation.setQingxiShebeiTiaoma(zsQingxi.getQingxiShebeiTiaoma());
            zsPackageImformation.setQingxiShenherenTiaoma(zsQingxi.getQingxiShenherenTiaoma());
            zsPackageImformation.setQingxiQingxirenTiaoma(zsQingxi.getQingxiQingxirenTiaoma());
        }
        zsPackageImformationService.updateByBatch(zsPackageImformationList);
        return map;
    }





    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> updateZsPackage(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();
        Date date = new Date();
        Date end = new Date();
        int i = 0;
        while (i<=28){
            date.setMonth(1);
            date.setDate(i);
            end.setMonth(1);
            end.setDate(i);
            date.setHours(00);
            end.setHours(23);
            map1.put("startTime",date);
            map1.put("endTime",end);
//            map1.put("fafangren","EMP1033");
            List<ZsPackageImformation> zsPackageImformationList = zsPackageImformationService.selectSinUp(map1);
            Random random = new Random();
            int qingxiRandomNum = random.nextInt(5)+17;
            int peibaoRandomNum = random.nextInt(5)+5;
            int miejunRandomNum = random.nextInt(10)+50;
            String qingxiLiushui = "";
            String miejunLiushui = "";
            int hashC = qingxiLiushui.hashCode();
            int hashM = miejunLiushui.hashCode();
            for(ZsPackageImformation zsPackageImformation:zsPackageImformationList){
                if(zsPackageImformation!=null){
                    qingxiLiushui = zsPackageImformation.getQingxiLiushui();
                    if(qingxiLiushui!=null){
                        if(qingxiLiushui.hashCode()!=hashC){
                            hashC= qingxiLiushui.hashCode();
                            qingxiRandomNum = random.nextInt(5)+20;
                            peibaoRandomNum = random.nextInt(5)+5;
                        }
                        Date qingxiDate = zsPackageImformation.getQingxiKaishiTime();
                        if(qingxiDate!=null){
//                            long qingxiEndTime = qingxiDate.getTime()+qingxiRandomNum*60*1000l;
//                            long peibaoTime = qingxiEndTime+peibaoRandomNum*60*1000l;
//                            zsPackageImformation.getQingxiLiushui();
//                            Date qingxiEnd = new Date(qingxiEndTime);
//                            Date peibaoEndTime = new Date(peibaoTime);
//                            zsPackageImformation.setQingxiJieshuTime(qingxiEnd);
//                            zsPackageImformation.setPeibaoTime(peibaoEndTime);
//                            zsPackageImformation.setPeibaoGuanbaoTime(peibaoEndTime);
//                            zsPackageImformation.setPeibaoShenherenTime(peibaoEndTime);
//                            zsPackageImformation.setQingxiShenherenTime(peibaoEndTime);
//                            miejunLiushui = zsPackageImformation.getMiejunLiushui();
                            //灭菌时间的处理
                            if(miejunLiushui !=null){
                                if(miejunLiushui.hashCode()!=hashM) {
                                    hashM = miejunLiushui.hashCode();
                                    miejunRandomNum = random.nextInt(10) + 50;
                                }
                                Date miejunKaishi = zsPackageImformation.getMiejunKaishiTime();
                                Date miejunJieshu = zsPackageImformation.getMiejunJieshuTime();
                                if(miejunKaishi!=null&&miejunJieshu!=null){
//                                    Date peibaoDate = zsPackageImformation.getPeibaoTime();
//
//                                    if(miejunKaishi.getTime()<peibaoDate.getTime()){
//                                        miejunKaishi.setTime(miejunKaishi.getTime()+30*60+1000l);
//                                        miejunJieshu.setTime(miejunJieshu.getTime()+30*60+1000l);
//                                    }
//                                    zsPackageImformation.setMiejunKaishiTime(miejunKaishi);
//                                    zsPackageImformation.setMiejunJieshuTime(miejunJieshu);
//                                    long chazhi = miejunJieshu.getTime()-miejunKaishi.getTime();
//                                    int chazhi1 = (int)chazhi;
//                                    if(chazhi1<=300000){
//                                        miejunJieshu.setTime(miejunJieshu.getTime()+miejunRandomNum*60*1000l);
//                                        zsPackageImformation.setMiejunJieshuTime(miejunJieshu);
//                                    }else if(chazhi1>=90*60*1000l){
//                                        miejunJieshu.setTime(miejunKaishi.getTime()+miejunRandomNum*60*1000l);
//                                    }
                                }
                                //发放记录的插入
                                String fafangliushui = zsPackageImformation.getFafangLiushui();
                                if(miejunJieshu!=null){
                                    String [] fafangren = new String[]{"EMP1032"};
                                    int a = random.nextInt(10);
                                    zsPackageImformation.setFafangrenTiaoma(fafangren[0]);
//                                    miejunJieshu.setHours(7);
//                                    Date fafang = miejunJieshu;
//                                    fafang.setHours(7);
//                                    fafang.setMinutes(28+a);
//                                    fafang.setSeconds(a);
//                                    fafang.setDate(miejunJieshu.getDate()+1);
                                    long fafangTime = miejunJieshu.getTime()+miejunRandomNum*60*1000l;
                                    Date fafangTimeD = new Date(fafangTime);
                                    zsPackageImformation.setFafangTime(fafangTimeD);
//                                    zsPackageImformation.setFafangTime(fafang);
                                }
                                if(fafangliushui==null){

                                }
                            }
                        }
                    }

                }
            }
            if(zsPackageImformationList.size()!=0){
                zsPackageImformationService.updateByBatch(zsPackageImformationList);
            }
            i++;
        }


        return map;
    }


}
